package com.health.viewscontrols.settings;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.health.R;
import com.health.adapter.DrugManagerAdapter;
import com.health.db.FDbManager;
import com.health.tablebean.BD_DD_DRUG;
import com.health.viewscontrols.NovaActivity;
import com.health.viewscontrols.NovaApplication;
import com.health.widget.CommItemDecoration;
import java.util.ArrayList;
import java.util.List;

/**
 * author : xiaoxin
 * date   : 2020/7/25 19:19
 * desc   : 搜索药品
 * update :
 */
public class QueryDrugListActivity extends NovaActivity {

    private static final String TAG = "QueryDrugListActivity";
    private DrugManagerAdapter drugManagerAdapter;
    private FDbManager fdbmanager = null;
    private LinearLayout ll_finish;
    private TextView tv_clear_drug;
    private EditText et_search_drug;
    private RecyclerView drugRecyclerView;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private List<BD_DD_DRUG> allInInfoDB = new ArrayList<>();
    private String type;
    private int drugPosition = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_drug_list);
        type = getIntent().getStringExtra("type");
        drugPosition = getIntent().getIntExtra("position", 0);
        fdbmanager = NovaApplication.fdbmanger;
        findViews();
        initAdapter();
        setListeners();

        mSwipeRefreshLayout.setRefreshing(true);
        queryDrugListData();

        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                queryDrugListData();
            }
        });

    }

    @Override
    public void findViews() {
        ll_finish            =   findViewById(R.id.ll_finish);
        tv_clear_drug        =   findViewById(R.id.tv_clear_drug);
        et_search_drug       =   findViewById(R.id.et_search_drug);
        drugRecyclerView     =   findViewById(R.id.drugRecyclerView);
        mSwipeRefreshLayout  =   findViewById(R.id.mSwipeRefreshLayout);
    }

    @Override
    public void setListeners() {
        drugManagerAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent();
                intent.putExtra("position", drugPosition);
                intent.putExtra("bean",(BD_DD_DRUG)adapter.getData().get(position));
                setResult(Activity.RESULT_OK,intent);
                finish();
            }
        });

        ll_finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_clear_drug.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_search_drug.setText("");
                queryDrugListData();
            }
        });

        et_search_drug.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s.toString())) {
                    filterData(s.toString());
                }else {
                    queryDrugListData();
                }
            }
        });
    }

    private void initAdapter() {
        drugManagerAdapter = new DrugManagerAdapter(R.layout.item_drug_list,allInInfoDB);
        drugRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        CommItemDecoration divider1 = CommItemDecoration.createVertical(this,
                Color.parseColor("#D2D2D2"),1, 0);
        drugRecyclerView.addItemDecoration(divider1);
        drugRecyclerView.setAdapter(drugManagerAdapter);
    }

    private void queryDrugListData() {
        allInInfoDB.clear();
        allInInfoDB = fdbmanager.findAllByWhereInInfoDB(BD_DD_DRUG.class,"type = '" + type +"'");
        drugManagerAdapter.setNewData(allInInfoDB);
        mSwipeRefreshLayout.setRefreshing(false);
    }

    private void filterData(String inputStr) {
        if (!TextUtils.isEmpty(inputStr)) {
            List<BD_DD_DRUG> filterDataList = new ArrayList<>();
            for (BD_DD_DRUG bdDdDrug : allInInfoDB) {
                String abbreviation = bdDdDrug.getAbbreviation();
                if (TextUtils.isEmpty(abbreviation)) continue;
                if (abbreviation.contains(",")) {
                    //如果药品有多个名称 需要分割循环校验
                    String[] split = abbreviation.split(",");
                    for (int i = 0; i < split.length; i++) {
                        if (!TextUtils.isEmpty(split[i]) && (
                                split[i].startsWith(inputStr.toLowerCase())
                                        || split[i].startsWith(inputStr.toUpperCase()) )) {
                            filterDataList.add(bdDdDrug);
                            break;
                        }
                    }
                }else {
                    if (!TextUtils.isEmpty(abbreviation) && (
                            abbreviation.startsWith(inputStr.toLowerCase())
                                    || abbreviation.startsWith(inputStr.toUpperCase()) )) {
                        filterDataList.add(bdDdDrug);
                    }
                }
            }
            drugManagerAdapter.setNewData(filterDataList);
        }
    }
}
